unit cuentaCorriente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Grids, DBGrids;

type
  TfcuentaCorriente = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    DBGrid1: TDBGrid;
    Label1: TLabel;
    Label2: TLabel;
    LabeledEdit1: TLabeledEdit;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    GroupBox1: TGroupBox;
    Button1: TButton;
    GroupBox2: TGroupBox;
    DBGrid2: TDBGrid;
    Panel1: TPanel;
    Panel2: TPanel;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    Panel3: TPanel;
    GroupBox5: TGroupBox;
    LabeledEdit4: TLabeledEdit;
    GroupBox6: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    GroupBox7: TGroupBox;
    LabeledEdit5: TLabeledEdit;
    GroupBox8: TGroupBox;
    LabeledEdit6: TLabeledEdit;
    Button2: TButton;
    Panel4: TPanel;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    Panel5: TPanel;
    GroupBox9: TGroupBox;
    LabeledEdit7: TLabeledEdit;
    GroupBox10: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    DateTimePicker5: TDateTimePicker;
    DateTimePicker6: TDateTimePicker;
    GroupBox11: TGroupBox;
    LabeledEdit8: TLabeledEdit;
    GroupBox12: TGroupBox;
    LabeledEdit9: TLabeledEdit;
    Button3: TButton;
    Panel6: TPanel;
    Label9: TLabel;
    Label10: TLabel;
    DBGrid3: TDBGrid;
    Panel7: TPanel;
    DBGrid4: TDBGrid;
    Panel8: TPanel;
    GroupBox13: TGroupBox;
    LabeledEdit10: TLabeledEdit;
    GroupBox14: TGroupBox;
    Label11: TLabel;
    Label12: TLabel;
    DateTimePicker7: TDateTimePicker;
    DateTimePicker8: TDateTimePicker;
    GroupBox15: TGroupBox;
    LabeledEdit11: TLabeledEdit;
    GroupBox16: TGroupBox;
    LabeledEdit12: TLabeledEdit;
    Button4: TButton;
    procedure LabeledEdit1Change(Sender: TObject);
    procedure LabeledEdit1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
    procedure limpiarFormulario;

  public
    { Public declarations }
     procedure cargarCliente;
     procedure cargarPagos;
  end;

var
  fcuentaCorriente: TfcuentaCorriente;

implementation

uses datamodule, verclientes, ingresarPago;
{$R *.dfm}

procedure TfcuentaCorriente.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
limpiarFormulario;
end;

procedure TfcuentaCorriente.FormShow(Sender: TObject);
begin
  datamodule1.QCCCliente.Active := true;
end;

procedure TfcuentaCorriente.LabeledEdit1Change(Sender: TObject);
begin
  cargarCliente;
  Button1.Enabled:=true;
end;

procedure TfcuentaCorriente.limpiarFormulario;
begin
  LabeledEdit1.Text := '';
  Label1.Caption := 'seleccione un cliente';
  datamodule1.QCCCliente.Active := false;
  datamodule1.QCCCliente.SQL.Clear;
  datamodule1.QCCCliente.SQL.Add('select * from venta v ' +
      'inner join detalle_venta d on v.id_venta = d.id_venta ' +
      'inner join cliente c on v.id_cliente = c.id_cliente ' +
      'where v.cc = 1 group by v.id_venta');
  datamodule1.QCCCliente.open;
  datamodule1.QCCCliente.Active := true;
  LabeledEdit2.Text := '0';
  LabeledEdit3.Text := '0';
  Button1.Enabled:=false;
end;

procedure TfcuentaCorriente.Button1Click(Sender: TObject);
begin
fingresarPago.ShowModal;
end;

procedure tfcuentacorriente.cargarPagos;
begin
  datamodule1.Qpagos.Active := false;
  datamodule1.Qpagos.SQL.Clear;
  datamodule1.Qpagos.SQL.Add('select * from pago p ' +
      'inner join cliente c on p.id_cliente = c.id_cliente');
  datamodule1.Qpagos.open;
  datamodule1.Qpagos.Active := true;
end;

procedure TfcuentaCorriente.cargarCliente;
begin
  // buscar con filtro cliente
  datamodule1.QCCCliente.Active := false;
  datamodule1.QCCCliente.SQL.Clear;
  datamodule1.QCCCliente.SQL.Add('select * from venta v ' + 'inner join detalle_venta d on v.id_venta = d.id_venta ' + 'inner join cliente c on v.id_cliente = c.id_cliente ' + 'where nombre_cli like ' + QuotedStr('%' + LabeledEdit1.Text + '%') + 'and v.cc = 1 group by v.id_venta');
  datamodule1.QCCCliente.open;
  datamodule1.QCCCliente.Active := true;
  Label2.Caption := LabeledEdit1.Text;
  // completar saldo cliente
  if datamodule1.cliente.FieldByName('saldo').AsFloat < 0 then
  begin
    // es saldo a favor del cliente
    LabeledEdit2.Text := floattostr((datamodule1.cliente.FieldByName('saldo').AsFloat) * -1);
    labelededit3.Text:='0';
  end
  else
  begin
    // es saldo a favor de cyc
    LabeledEdit3.Text := floattostr(datamodule1.cliente.FieldByName('saldo').AsFloat);
    labelededit2.Text:='0';
  end;
end;

procedure TfcuentaCorriente.LabeledEdit1Click(Sender: TObject);
begin
  fverclientes.ShowModal;
  LabeledEdit1.Text := datamodule1.cliente.FieldByName('nombre_cli').AsString;
end;

end.
